#!/usr/bin/bash
version="1.0.0-ok1"
basedir=$(cd `dirname $0`; pwd)
cd $basedir
echo "check and install dependencies..."
deps=(debhelper build-essential g++ cmake qtbase5-dev qt5-qmake qttools5-dev qttools5-dev-tools libkysdk-applications-dev libavcodec-dev libavformat-dev libavutil-dev libgsettings-qt-dev libkf5windowsystem-dev libkf5windowsystem5 libkysdk-qtwidgets-dev libkysdk-waylandhelper-dev libpulse-dev libqt5svg5-dev libqt5x11extras5 libqt5x11extras5-dev libswscale-dev libukui-log4qt-dev libx11-dev libxfixes-dev libxfixes3 libxinerama-dev libxinerama1 libxtst-dev libopenexr-dev debmake)
apt list --installed 2> /dev/null > ./.temp
uninstalled=false
for dep in ${deps[@]}
do
	if ! grep -E ^$dep/ ./.temp
	then
		echo "$dep is not installed"
		uninstalled=true
		break
	fi
done
rm ./.temp
if $uninstalled
then
	sudo apt install -y ${deps[*]} 2> /dev/null && echo "Successfully install dependencies"
fi

cd $basedir/../live/res
echo "downloading traced_quantized_bert.pt..."
let sizeorig=407285494
md5orig="fa72a8c0603d1dd6c08036a91509bc09"
while true
do
if [ ! -f traced_quantized_bert.pt ]
then
	wget -c https://github.com/XZZ-prior/BERT_NER_weights/raw/master/traced_quantized_bert.pt -O traced_quantized_bert.pt >& 2 && echo "Successfully download traced_quantized_bert.pt"
else
	let fileSize=$(du -b traced_quantized_bert.pt | grep -E -o [0-9]+ | head -n 1)
	if [ $fileSize -lt $sizeorig ]
	then
		wget -c https://github.com/XZZ-prior/BERT_NER_weights/raw/master/traced_quantized_bert.pt -O traced_quantized_bert.pt >& 2 && echo "Successfully download traced_quantized_bert.pt"
	elif [ $fileSize -eq $sizeorig ]
	then
		echo "verify file integrity..."	
		result=$(md5sum traced_quantized_bert.pt)
		compare=$(echo $result | grep $md5orig)
		if [[ "$compare" != "" ]]
		then
			echo "verify file integrity...yes"	
			break
		else
			echo "verify file integrity...failed"	
			rm traced_quantized_bert.pt && echo "Remove corrupted file traced_quantized_bert.pt"
		fi
	else
		rm traced_quantized_bert.pt && echo "Remove corrupted file traced_quantized_bert.pt"
	fi
fi
done

cd $basedir/..
architecture=$(uname -m)
case $architecture in
  x86_64|i686|i386)
    echo "系统是x86架构。"
	rm -rf build
	mkdir build
	cd build
	cmake .. && make && sudo make install
    ;;
  riscv64)
	
	sudo mkdir -p /usr/share/live-image-text
    sudo cp -f ./live/riscv/opencv4.9/lib64/lib* /usr/lib/
	sudo cp -f ./live/riscv/fast_tokenizer/lib/libcore_tokenizers.so /usr/lib/
	sudo cp -r ./live/inference /usr/share/live-image-text/
	sudo cp -r ./live/res /usr/share/live-image-text/
	sudo cp ./build/live-image-text /usr/bin/
	cd './大赛提交材料/whl安装包'
	pip install torch-2.3.0a0+gitafe6d27-cp311-cp311-linux_riscv64.whl
	str=~/.local/lib/python3.11/site-packages/torch/lib
	if [[ $LD_LIBRARY_PATH =~ $str ]]
	then
		:
	else
		export LD_LIBRARY_PATH=$str:$LD_LIBRARY_PATH
	fi
	if grep "export LD_LIBRARY_PATH=$str:" ~/.bashrc > /dev/null
	then
		:
	else
		echo export LD_LIBRARY_PATH=$str:'$LD_LIBRARY_PATH' >> ~/.bashrc
	fi
	
    ;;
  *)
    echo "未知架构：$architecture"
    ;;
esac

read -n1 -s -p "install finished, press any key to run live-image text"
live-image-text
